package com.bdjk.bdmw.InfoGath.business.nurse.dao.impl;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import com.bdjk.bdmw.InfoGath.business.iparent.model.IparentPositionModel;
import com.bdjk.bdmw.InfoGath.business.iparent.vo.IparentPositionVO;
import com.bdjk.bdmw.InfoGath.business.nurse.dao.NursePositionDAO;
import com.bdjk.bdmw.InfoGath.business.nurse.model.NursePositionModel;
import com.bdjk.bdmw.InfoGath.business.nurse.vo.NursePositionVO;
import com.bdjk.bdmw.InfoGath.common.dao.impl.JdbcDAOImpl;
import com.bdjk.bdmw.InfoGath.exception.BusinessException;

public class NursePositionDAOImpl extends JdbcDAOImpl implements NursePositionDAO {

	
	public List<NursePositionModel> query(String sql,List<Object> paramList) throws BusinessException {
		List<NursePositionModel> result=new ArrayList<NursePositionModel>();
		List<Object> sqlrs=executeResultSQL(sql.toString(),paramList);
		for(int i=0;i<sqlrs.size();i++){
			NursePositionVO vo=new NursePositionVO();
			List<Object> rs=(List<Object>)sqlrs.get(i);
			int col=0;
			vo.setId(rs.get(col).toString());col++;
			vo.setTerIdent(rs.get(col).toString());col++;
			vo.setSendTime(rs.get(col).toString());col++;
			vo.setLongitude(rs.get(col).toString());col++;
			vo.setLatitude(rs.get(col).toString());col++;
			vo.setAltitude(rs.get(col).toString());col++;
			vo.setSpeed(rs.get(col).toString());col++;
			vo.setDirection(rs.get(col).toString());col++;
			NursePositionModel model=new NursePositionModel();
			try {
				model = vo.vo2Model();
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			result.add(model);
		}
		return result;
	}
	@Override
	public int savePosition(NursePositionModel model) {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
				StringBuffer sql=new StringBuffer();
				sql.append("insert into t_position (ter_ident,send_time,longitude,latitude,altitude,speed,direction)values(?,?,?,?,?,?,?)");
				List<Object> params = new ArrayList<Object>();
				params.add(model.getTerIdent());
				params.add(model.getSendTime());
				params.add(model.getLongitude());
				params.add(model.getLatitude());
		        params.add(model.getAltitude());
				params.add(model.getSpeed());
				params.add(model.getDirection());
				int positionId=0;
				try {
					positionId=executeSQL(sql.toString(), params);
				} catch (BusinessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return positionId;
	}

	@Override
	public int saveExtension(NursePositionModel model, int id) {
		// TODO Auto-generated method stub
		System.out.println(id);
		StringBuffer sql=new StringBuffer();
		sql.append("insert into t_position_extension (position_id,ter_ident,sendTime,baseInfo,qy_version,qy_encryptType,qy_validatCode,qy_terType,qy_timeZone,qy_mesType,qy_collectMills)values(?,?,?,?,?,?,?,?,?,?,?)");
		List<Object> params = new ArrayList<Object>();
		params.add(id);
		params.add(model.getTerIdent());
		params.add(model.getSendTime());
		params.add(model.getBaseInfo());
		params.add(model.getProVersion());
		params.add(model.getEncryptType());
		params.add(model.getValidatCode());
        params.add(model.getTerType());
		params.add(model.getTimeZone());
		params.add(model.getMesType());
		params.add(model.getCollectMills());
		
		try {
			executeSQL(sql.toString(), params);
		} catch (BusinessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
	}

	@Override
	public List<NursePositionModel> queryPosition(List<Object> paramList)
			throws BusinessException {
		// TODO Auto-generated method stub
		String sql="SELECT * FROM t_position WHERE UNIX_TIMESTAMP( send_time ) BETWEEN UNIX_TIMESTAMP( ?) AND UNIX_TIMESTAMP( ? ) AND ter_ident=?";
		List<NursePositionModel> result=query(sql,paramList);
		return result;
	}

	@Override
	public NursePositionModel queryCurrentPosition(List<Object> paramList)
			throws BusinessException {
		// TODO Auto-generated method stub
		NursePositionModel model=null;
		String sql="SELECT * FROM (SELECT *  FROM t_position WHERE ter_ident=?)t ORDER BY send_time DESC LIMIT 0,1";	
		List<NursePositionModel> result=query(sql,paramList);
		if(result.size()>0){
			model=result.get(0);
		}
		return model;
	}

}
